package com.mera.tektronix.csviewer.controllers

import org.apache.commons.logging.LogFactory

import com.mera.tektronix.csviewer.utils.*

class DispatcherController {
	private static final log = LogFactory.getLog(DispatcherController.class)
	
    def getList() {		
		if (params.node == "_treeRoot_") {
			redirect(controller: "department", action: "getList", params : params)
		} else if (params.node =~ "_DEP_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node)
			params.id = dbId
			redirect(controller: "project", action: "getList", params: params)
		} else if (params.node =~ "_PRJ_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node)
			params.id = dbId
			redirect(controller: "employee", action: "getList", params: params)
		}
	}
	
	def getAllList() {
		if (params.node =~ "_DEP_") {
			redirect(controller: "department", action: "getAllList", params : params)
		} else if (params.node =~ "_PRJ_") {
			redirect(controller: "project", action: "getAllList", params : params)
		}
	}
	
	def getElement() {		
		if (params.node == "_treeRoot_") {			
			if(params.nodeName != null) {
				params.companyName = params.nodeName
			} else {
				params.companyId = 1
			}

			redirect(controller: "company", action: "getElement", params: params)
		}
		else if (params.node =~ "_DEP_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node);
			params.id = dbId
			redirect(controller: "department", action: "getElement", params: params)
		}
		else if (params.node =~ "_PRJ_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node);
			params.id = dbId
			redirect(controller: "project", action: "getElement", params: params)
		}
		else {
			Long dbId = DBUtils.nodeIdToDBId(params.node);
			params.id = dbId
			redirect(controller: "employee", action: "getElement", params: params)
		}
	}
	
	def addElement() {		
		if (params.parentNode == "_treeRoot_") {
			if(params.parentNodeName != null) {
				params.companyName = params.parentNodeName
			} else {
				params.companyId = 1
			}
			redirect(controller: "department", action: "addElement", params: params)
		}
		else if (params.parentNode =~ "_DEP_") {
			Long parentNode = DBUtils.nodeIdToDBId(params.parentNode)
			params.department = parentNode
			redirect(controller: "project", action: "addElement", params: params)
		}
		else if (params.parentNode =~ "_PRJ_") {
			Long parentNode = DBUtils.nodeIdToDBId(params.parentNode)
			params.project = parentNode
			redirect(controller: "employee", action: "addElement", params: params)
		}
	}
	
	def updateElement() {		
		if (params.node =~ "_DEP_") {
			if(params.parentNodeName != null) {
				params.companyName = params.parentNodeName
			} else {
				params.companyId = 1
			}
			Long dbId = DBUtils.nodeIdToDBId(params.node)
			params.id = dbId
			redirect(controller: "department", action: "updateElement", params: params)
		}
		else if (params.node =~ "_PRJ_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node)
			params.id = dbId
			redirect(controller: "project", action: "updateElement", params: params)
		}
		else if (params.node =~ "_EMP_") {
			Long dbId = DBUtils.nodeIdToDBId(params.node)
			params.id = dbId
			redirect(controller: "employee", action: "updateElement", params: params)
		}
	}
	
	def deleteElement() {		
		if (params.node != "_treeRoot_") {
			if (params.node =~ "_DEP_") {
				Long dbId = DBUtils.nodeIdToDBId(params.node)
				params.id = dbId
				redirect(controller: "department", action: "deleteElement", params: params)
			}
			else if (params.node =~ "_PRJ_") {
				Long dbId = DBUtils.nodeIdToDBId(params.node)
				params.id = dbId
				redirect(controller: "project", action: "deleteElement", params: params)
			}
			else {
				Long dbId = DBUtils.nodeIdToDBId(params.node)
				params.id = dbId
				redirect(controller: "employee", action: "deleteElement", params: params)
			}
		}
	}
	
	def redir() {
		redirect(uri: "/")
	}
}
